<?php

include ("config.php");


class Connect
{
	var $m_conf;
	var $b_conf;	
	function __construct() {
		global $mysql_conf,$db_conf;
		$this->m_conf = $mysql_conf;
		$this->b_conf = $db_conf;
	}
	function Query($query, $db) {
		$conn_id = mysql_connect($this->m_conf['dbhost'],$this->m_conf['dbuser'],$this->m_conf['dbpass']) or die("Could not Connect");
		mysql_select_db($this->b_conf["$db"]) or die("Could not select Database ".$db."!");
		return $do_query = mysql_query($query,$conn_id);
		settype($do_query, "null"); 
		mysql_close($conn_id); 
	}
}

class Character extends Connect
{
	function __construct() {
		parent::__construct();
	}
	function ChechCharInAcc($charguid, $uid) {
		$db = sprintf("chardb%d",$_SESSION["realmid"]);
		$q  = "SELECT * FROM `characters` WHERE `guid`='%d' AND `account`='%d'";
		$query = sprintf($q, $charguid, $uid);
		$select = $this->Query($query,$db);
		$count = mysql_num_rows($select);
		if($count==1) return true; else return false;
	}
	function PrintCharlist($accguid) {
		$db = sprintf("chardb%d",$_SESSION["realmid"]);
		$q = "SELECT `guid`,`name`,`race`,`class`,`gender`,`level` FROM `characters` WHERE `account`='%d'";
		$query = sprintf($q, $accguid);
		$chararray = $this->Query($query, $db);
		echo "<table>";
		echo "<tr><td>Имя</td><td>Класс</td><td>Раса</td><td>Уровень</td></tr>";
		while($chars = mysql_fetch_array($chararray)) {
			echo "<tr><td><a href=?do=main&chr=".$chars['guid'].">".$chars['name']."</a></td><td><img src=./template/default/img/class/".$chars['class'].".png></td><td><img src=./template/default/img/race/".$chars['race']."-".$chars['gender'].".png></td><td>".$chars['level']."</td></tr>";
		}
		echo "</table>"; 
		mysql_free_result($chararray);		
	}
	function PrintCharsinfo($charguid) {
		settype($charguid,"int");
		$uid = $_SESSION["uid"];
		if($this->ChechCharInAcc($charguid,$uid)) {
		if($charguid<1) return false;
		else {
			$db = sprintf("chardb%d",$_SESSION["realmid"]);
			$q = "SELECT * FROM `characters` WHERE `guid`='%d'";
			$query = sprintf($q, $charguid);
			$select = $this->Query($query,$db);
			$chararray = mysql_fetch_array($select);
			
			// calculate gold
			$money = $chararray['money'];
			$money_g = (int)($money/10000);
			$total_money = $money - ($money_g*10000);
			$money_s = (int)($total_money/100);
			$money_c = $total_money - ($money_s*100);
			
			echo "<center><table>";
			echo "<tr><td>Имя</td><td>Класс</td><td>Раса</td><td>Уровень</td><td>Очков чести</td><td>Деньги</td><td>Исправить</td></tr>";
			echo "<tr><td>".$chararray['name']."</td><td><img src=./template/default/img/class/".$chararray['class'].".png></td><td><img src=./template/default/img/race/".$chararray['race']."-".$chararray['gender'].".png></td><td>".$chararray['level']."</td><td>".$chararray['totalHonorPoints']."</td><td>".$money_g."  з".$money_s." с".$money_c." м</td><td><input type='button' onclick=location.href='?do=main&chr=$charguid&unbug=1' value='Исправить'></td></tr>";
			echo "</table></center>";		
		} }
	}
	function CharFix($charguid)	{
		$uid = $_SESSION["uid"];
		$db = sprintf("chardb%d",$_SESSION["realmid"]);
		$q  = "SELECT * FROM `character_homebind` WHERE `guid`='%d'";
		$query = sprintf($q, $charguid);
		$select = $this->Query($query,$db);
		$chars = mysql_fetch_array($select);
		$px = $chars['position_x']; $py = $chars['position_y']; $pz = $chars['position_z']; $pm = $chars['map'];
		if($this->ChechCharInAcc($charguid, $uid)) {
			$this->Query("UPDATE `characters` SET `position_x`='$px', `position_y`='$py', `position_z`='$pz', `map`='$pm' WHERE `guid`='$charguid'", $db);
			$this->Query("DELETE * FROM `character_aura` WHERE `guid`='$charguid'", $db);
			$this->Query("DELETE * FROM `group_member` WHERE `memberGuid`='$charguid'", $db);
			return "<center><b>Персонаж исправлен!</b></center>";
		} else return "<center><b>Ошибка!</b></center>";
	}

}

class Account extends Connect
{
	function __construct() {
		parent::__construct();
	}
	function Chechban($charguid) {
	$select = $this->Query("SELECT * FROM `account_banned` WHERE `id`=$charguid","authdb");
		if(mysql_num_rows($select)!=0) {
			$array = mysql_fetch_array($select);
			$active = $array['active'];
			if($active==0) return false;
			else return true;
		}
		else return false;
	}
}
	
$HOST = new Connect;		
$cChar = new Character;
$cAcc = new Account;

?>